!function(window,document,$,undefined) {
  
   var library = {
    
       // 图书分类
      classify:[
         {"id":1,name:'历史类'},
         {"id":2,name:'军事类'},
         {"id":3,name:'科学类'},
         {"id":4,name:'小说类'}
      ],

       // 初始化入口 //
       
       init:function() {
          this.initEvent();
          this.initTable();
          this.upDateTime();
          this.renderClassify();
          this.btnDisabled();

       },

       btnDisabled:function() {
            $('#updateBtn').add($('#delBtn')).prop('disabled','disabled');
       },


       // 渲染列对话框中的图书分类项

       renderClassify:function() {
          var data = this.classify,arr = [];
              data.unshift({id:0,name:'请选择'});
          $.each(data,function(key,obj) {
              arr.push('<option value="'+obj.id+'">' + obj.name + '</option>');
          })
          $('#classify').html(arr.join(''));
         
       },

       // 事件入口

       initEvent:function() {
         $('#addBtn').on('click',this.onAddBtnClick);
         $('#saveBtn').on('click',this.onSaveBtnClick);
         $('input[name="status"]').on('click',this.onChangeClick);
         $('#tbody').on('click','input[type="checkbox"]',this.onCheckedCheckbox);
       },

       // 决定单选框是否被选中
         onCheckedCheckbox:function() {
          
            if($('.text input[type="checkbox"]:checked').length > 0) {
                 $('#updateBtn').add($('#delBtn')).removeProp('disabled');
             }else {
                 library.btnDisabled();
             }
         },

       // 日期组件 

       upDateTime:function() {
          $('#p_date').datetimepicker({
              language:  'zh-CN',
              weekStart: 1,
              todayBtn:  1,
              autoclose: 1,
              todayHighlight: 1,
              startView: 2,
              minView: 2,
              forceParse: 0
              });
       },

       onChangeClick:function() {
          
          if(parseInt($('input[name="status"]:checked').val()) == 0) {
                 $('input[name="borrowStatus"][value="1"]').prop('disabled','disabled');
                 $('input[name="borrowStatus"][value="0"]').trigger('click');
             }else {
                 $('input[name="borrowStatus"][value="1"]').removeProp('disabled');
             }
       },

       // 新增按钮

       onAddBtnClick:function() {
          $('#dialog').modal('show');
          $('#saveBtn').removeClass('submit');
       },

       // 新增保存的事件

       onSaveBtnClick:function() {
           var $this = $(this),param;

             if($this.hasClass('submit')) {
                return 
             }
          
          // TODO 进行表单验证
            param = {
              name:$('#name').val(),
              author:$('#author').val(),
              price: $('#price').val(),
              p_date:$('#p_date').val(),
              publisher:$('#publisher').val(),
              classify:$('#classify').val(),
              status:$('input[name="status"]:checked').val(),
              borrow_status:$('input[name="borrowStatus"]:checked').val()

           },
           url = "api/books_add.php";
            $this.addClass('submit');
           $.get(url,param,function(response) {
               
               if(response.success) {
                  library.restTable();
               }else {
                 alert('网络连接失败')
               }
              
               $('#dialog').modal('hide');
                library.initTable();
             
           },'json')
          
       },

       
       initTable:function() {

           var url = 'api/books_list.php';
            $('.masker').show();
           $.get(url,function(response) {
              
               var data = response.data;
                 library.renderTable(data);
                $('.masker').hide();
           },'json')
       },

       renderTable:function(data) {
          var arr = [];

          $.each(data,function(key,obj) {
            var borrow_status = obj.borrow_status,
                borrow_status_map = {0:'未借出',1:'已借出'},
                p_date = obj.p_date.split(' ')[0],
                classify = 
                     {
                    0:'未分类',
                    1:'历史类',
                    2:'军事类',
                    3:'科学类',
                    4:'小说类'
                    };
                   
             arr.push(
                   '<tr>',
                     '<td class="text" ><input type="checkbox"></td>',
                     '<td>',obj.name,'</td>',
                     '<td>',obj.author,'</td>',
                     '<td>',obj.publisher,'</td>',
                     '<td>￥',obj.price + '.00' + '</td>',
                     '<td>',p_date,'</td>',
                     '<td>',classify[obj.classify] || classify[0],'</td>',
                     '<td>',obj.status? '上架':'下架','</td>',
                     '<td>',borrow_status_map[borrow_status],'</td>',
                   '</tr>'
                );

             $('#tbody').html(arr.join(''));
          })
       },

       restTable:function() {
           $('#libraryForm').trigger('reset');
       }

   }


    library.init();
}(window,document,jQuery)